package com.purong.purong.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.purong.purong.Contant;
import com.purong.purong.R;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wenjiezeng on 2017/11/10.
 */
public class LoanAskQuestActivity extends BaseActivity{
    private EditText questEd;
    private String agencyId;
    private Button confirmBtn;
    private boolean input;
    @Override
    public int getRootViewId() {
        return R.layout.activity_ask_question;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("问题");
        questEd=(EditText)findViewById(R.id.edit_text);
        confirmBtn=(Button)findViewById(R.id.confirm);
        questEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    input = false;
                } else {
                    input = true;
                }
                if (input) {
                    confirmBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirmBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initBundle() {
        agencyId=getIntent().getStringExtra("AGENCY_ID");
    }

    @Override
    public void initData() {

    }
    private void question(){
        if (agencyId==null)
            return;
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("loan_agency_id",agencyId);
        params.put("question_content",questEd.getText().toString().trim());
        request(Contant.LOAN_QUESTION_SUBMIT, params, true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOAN_QUESTION_SUBMIT)){
            if (responseInfo.isRequestOk){
                showToast("提问成功");
                this.finish();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (!input){
                    showToast("问题内容不能为空");
                    return;
                }
                question();
                break;
        }
    }
}
